<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="项目名：" prop="name">
        {{ this.queryParams.projectName }}
      </el-form-item>

      <el-form-item label="路线名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入路线名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出行区域" prop="area">
        <el-select v-model="queryParams.area" placeholder="请选择区域">
          <el-option key="1" label="省内" value="省内"></el-option>
          <el-option key="2" label="省外" value="省外"></el-option>
          <el-option key="3" label="国外" value="国外"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否为专属路线" prop="allowUnified">
        <el-select
          v-model="queryParams.allowUnified"
          placeholder="是否为专属路线"
        >
          <el-option key="1" label="是" value="是"></el-option>
          <el-option key="2" label="否" value="否"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否允许携带家属" prop="canBring">
        <el-select v-model="queryParams.canBring" placeholder="是否为专属路线">
          <el-option key="1" label="允许" value="允许"></el-option>
          <el-option key="2" label="不允许" value="不允许"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态">
          <el-option key="0" label="未提交" value="未提交"></el-option>
          <el-option key="1" label="待审核" value="待审核"></el-option>
          <el-option key="2" label="未通过" value="未通过"></el-option>
          <el-option key="3" label="已通过" value="已通过"></el-option>
          <el-option key="4" label="选为问卷" value="选为问卷"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['lxy:route:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['lxy:route:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['lxy:route:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="multiple"
          @click="handleQuestionnaireAdd"
          v-hasPermi="['lxy:questionnaire:add']"
          >创建问卷</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="multiple"
          @click="handleQuestionnaireRouteAdd"
          v-hasPermi="['lxy:questionnaire:add']"
          >在已有问卷中添加路线</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="routeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="路线名" align="center" prop="name" />
      <el-table-column label="描述" align="center" prop="content" />
      <el-table-column label="注意事项" align="center" prop="notice" />
      <el-table-column label="链接地址" align="center" prop="url" />
      <el-table-column label="出行区域" align="center" prop="area" />
      <el-table-column label="预算" align="center" prop="budget" />
      <el-table-column label="允许携带家属" align="center" prop="canBring" />
      <el-table-column label="是否专属" align="center" prop="allowUnified" />
      <el-table-column
        label="专属部门"
        align="center"
        prop="departmentNoCode"
      />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            v-hasPermi="['lxy:route:edit']"
            >查看详情</el-button
          >

          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleTime(scope.row)"
            v-hasPermi="['lxy:route:edit']"
            >查看时间段</el-button
          >

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['lxy:route:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['lxy:route:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改路线对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        v-if="type === 0"
      >
        <el-form-item label="项目名" prop="projectId">
          {{ this.queryParams.projectName }}
        </el-form-item>
        <el-form-item label="旅行社" prop="travelagenId">
          <el-select v-model="form.travelagenId" placeholder="请选择旅行社">
            <el-option
              v-for="item in travelagenOptions"
              :key="item.id"
              :label="item.agencyName"
              :value="item.id"
              :multiple="false"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="路线名" prop="name">
          <el-input v-model="form.name" placeholder="请输入路线名" />
        </el-form-item>
        <el-form-item label="描述">
          <editor v-model="form.content" :min-height="192" />
        </el-form-item>
        <el-form-item label="注意事项" prop="notice">
          <el-input
            v-model="form.notice"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="链接详情地址" prop="url">
          <el-input v-model="form.url" placeholder="请输入链接详情地址" />
        </el-form-item>
        <el-form-item label="选择出行区域" prop="area">
          <el-radio-group v-model="form.area">
            <el-radio label="省内">省内</el-radio>
            <el-radio label="省外">省外</el-radio>
            <el-radio label="国外">国外</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="预算" prop="budget">
          <el-input v-model="form.budget" placeholder="请输入预算" />
        </el-form-item>
        <el-form-item label="携带家属" prop="canBring">
          <el-radio-group v-model="form.canBring">
            <el-radio label="允许">允许</el-radio>
            <el-radio label="不允许">不允许</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否专属" prop="allowUnified">
          <el-radio-group v-model="form.allowUnified">
            <el-radio label="是">是</el-radio>
            <el-radio label="否">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="专属部门"
          prop="departmentNoArray"
          v-if="form.allowUnified === '是'"
        >
          <el-select
            v-model="departmentNoArray"
            multiple
            filterable
            placeholder="请选择专属部门"
          >
            <el-option
              v-for="item in departmentOptions"
              :key="item.dm"
              :label="item.dwmc"
              :value="item.dm"
            >
              <span style="float: left">{{ item.dwmc }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{
                item.dm
              }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <!-- 修改状态 -->
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="未提交">未提交</el-radio>
            <el-radio label="待审核">待审核</el-radio>
            <el-radio label="未通过">未通过</el-radio>
            <el-radio label="已通过">已通过</el-radio>
            <el-radio label="选为问卷">选为问卷</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="未通过理由"
          prop="noPassReason"
          v-if="form.status === '未通过'"
        >
          <el-input
            v-model="form.noPassReason"
            placeholder="请输入路线未通过理由"
          />
        </el-form-item>
        <el-form-item label="照片集,oss" prop="pic">
          <image-upload v-model="form.pic" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="type === 0">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
      <!-- 添加问卷 -->
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        v-if="type === 1"
      >
        <el-form-item label="项目名" prop="projectId">
          {{ this.queryParams.projectName }}
        </el-form-item>
        <el-form-item label="问卷名称" prop="questionnaireName">
          <el-input
            v-model="form.questionnaireName"
            placeholder="请输入问卷名称"
          />
        </el-form-item>
        <!-- <el-form-item label="问卷年份" prop="year">
          <el-input v-model="form.year" placeholder="请输入问卷年份" />
        </el-form-item> -->
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker
            clearable
            v-model="form.startTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择报名开始时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="截止时间" prop="endTime">
          <el-date-picker
            clearable
            v-model="form.endTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择报名截止时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="每人票数" prop="voteNum">
          <el-input v-model="form.voteNum" placeholder="请输入每人投票数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="type === 1">
        <el-button
          :loading="buttonLoading"
          type="primary"
          @click="submitQuestionnaireForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRoute,
  getRoute,
  delRoute,
  addRoute,
  updateRoute,
  addQuestionnaireRoute,
} from "@/api/lxy/route";
import { listTravelagen } from "@/api/lxy/travelagen";
import { addQuestionnaire, ifQuestionnaire } from "@/api/lxy/questionnaire";
import { listDepartment } from "@/api/lxy/zust";

export default {
  name: "Route",
  data() {
    return {
      //form中departmentNo获取得数组
      departmentNoArray: [],
      // 专属部门选项列表
      departmentOptions: [],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 路线表格数据
      routeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        area: undefined,
        departmentNo: undefined,
        status: undefined,
      },
      questionnaireId: undefined,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [{ required: false, message: "路线表不能为空", trigger: "blur" }],
        // projectId: [
        //   { required: true, message: "对应项目id不能为空", trigger: "blur" }
        // ],
        travelagenId: [
          { required: false, message: "旅行社不能为空", trigger: "blur" },
        ],
        name: [{ required: true, message: "路线名不能为空", trigger: "blur" }],
        content: [{ required: true, message: "描述不能为空", trigger: "blur" }],
        area: [
          { required: true, message: "出行路线不能为空", trigger: "blur" },
        ],
        canBring: [
          {
            required: true,
            message: "是否允许携带家属不能为空",
            trigger: "blur",
          },
        ],
        questionnaireName: [
          { required: true, message: "问卷名不能为空", trigger: "blur" },
        ],
        startTime: [
          { required: true, message: "报名开始时间不能为空", trigger: "blur" },
        ],
        endTime: [
          { required: true, message: "报名截止时间不能为空", trigger: "blur" },
        ],
        voteNum: [
          { required: true, message: "每人票数不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getListTravelagen();
    this.ifQuestionnaire();

    //获取专属部门单位选项列表
    listDepartment().then((response) => {
      this.departmentOptions = response.rows;
    });
  },
  methods: {
    /** 查询路线列表 */
    getList() {
      this.queryParams.projectName = this.$route.query.projectName;
      this.queryParams.projectId = this.$route.query.projectId;

      this.loading = true;
      listRoute(this.queryParams).then((response) => {
        this.routeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //判断该路线是否被选为问卷，0为未选，1为选
    ifQuestionnaire() {
      ifQuestionnaire(this.queryParams.projectId).then((response) => {
        if (response) this.questionnaireId = response;
      });
    },
    /** 跳转到路线对应的时间段 */
    handleTime(row) {
      if (row.status == "已通过" || row.status == "选为问卷") {
        const id = row.id;
        this.$router.push({
          path: "/lxy/time/",
          query: {
            projectId: row.projectId,
            routeId: row.id,
          },
        });
      } else {
        this.$modal.msgWarning("仅已通过和选为问卷的路线可以查看时间段！");
      }
    },

    handleDetail(row) {
      const id = row.id;
      this.$router.push({
        path: "/lxy/route/detail/show/" + id,
        query: {
          projectId: row.projectId,
          routeId: row.id,
        },
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        projectId: undefined,
        travelagenId: undefined,
        name: undefined,
        content: undefined,
        notice: undefined,
        url: undefined,
        area: undefined,
        budget: undefined,
        canBring: undefined,
        allowUnified: undefined,
        departmentNo: undefined,
        status: undefined,
        pic: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        questionnaireName: undefined,
        year: undefined,
        startTime: undefined,
        endTime: undefined,
        voteNum: undefined,
        questionnaireId: undefined,
        noPassReason: undefined,
        ids: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.type = 0;
      this.title = "添加路线";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      this.type = 0;
      const id = row.id || this.ids;
      getRoute(id).then((response) => {
        this.loading = false;
        this.form = response.data.agency;
        if (this.form.allowUnified === "是") {
          this.departmentNoArray = this.form.departmentNo.split(",");
        } else {
          this.departmentNoArray = [];
        }
        this.travelagenOptions = response.data.travelagen;
        this.open = true;
        this.title = "修改路线";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.form.allowUnified === "是") {
        this.form.departmentNo = this.departmentNoArray.join(",");
      } else {
        this.form.departmentNo = "";
      }
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateRoute(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            this.form.projectId = this.queryParams.projectId;
            addRoute(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除路线编号为"' + ids + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delRoute(ids);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
    /** 添加调查问卷 */
    handleQuestionnaireAdd(row) {
      const ids = row.id || this.ids;
      this.reset();

      if (this.questionnaireId != null) {
        this.open = false;
        this.$modal.msgSuccess("该项目已存在问卷");
      } else {
        this.type = 1;
        this.open = true;
        this.title = "添加问卷";
      }
    },
    // 问卷发布
    submitQuestionnaireForm() {
      this.form.projectId = this.queryParams.projectId;
      this.form.name = this.form.questionnaireName;
      addQuestionnaire(this.form, this.ids)
        .then((response) => {
          this.$modal.msgSuccess("问卷发布成功");
          this.open = false;
          this.getList();
        })
        .finally(() => {
          this.buttonLoading = false;
        });
    },
    /**查询旅行社列表 */
    getListTravelagen() {
      this.queryParams.params = {};
      listTravelagen(this.queryParams).then((response) => {
        this.travelagenOptions = response.rows;
      });
    },
    /** 在已有问卷中添加路线 */
    handleQuestionnaireRouteAdd(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否往该项目的问卷添加路线")
        .then(() => {
          this.loading = true;
          addQuestionnaireRoute(ids, this.questionnaireId);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("添加成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
  },
};
</script>
